﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using XFSale.Lib;
using XFSale.UI;

namespace XFSale.UI.Acct
{
    public partial class FrmTransAcctOpen : FrmBase
    {
        public FrmTransAcctOpen()
        {
            InitializeComponent();
        }

        private void HandleResponse(Pack response)
        {
            if (response.GetBaseByName("ReturnCode") == "0000")
            {
                txtOrderSheetNo.Text = response.GetRecordByName("OrderSheetSerialNo");
                MessageBox.Show(response.GetBaseByName("ReturnMsg"));
            }
            else
            {
                MessageBox.Show(response.GetBaseByName("ReturnMsg"));
            }
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            string password = string.Empty;
            WinPassword frmPassword = new WinPassword();
            DialogResult result = frmPassword.ShowDialog();
            if (result == DialogResult.Cancel)
            {
                frmPassword.Dispose();
                return;
            }

            if (result == DialogResult.OK)
            {
                password = frmPassword.Password;
                frmPassword.Dispose();
            }

            try
            {
                //初始化请求包
                Log.LogDebug("初始化请求包");
                request.ServiceCode = "101201";
                request.PackType = "R";
                request.Init();

                //填充请求包基础数据
                Log.LogDebug("填充请求包基础数据");
                request.SetBaseByName("ServiceCode", "101201");
                request.SetBaseByName("ChannelCode", "001");
                request.SetBaseByName("RegionCode", "0001");
                request.SetBaseByName("BranchCode", "000100001");
                request.SetBaseByName("AcceptMethod", "0");
                request.SetBaseByName("OpID", "00000001");

                //填充请求包记录数据
                Log.LogDebug("填充请求包记录数据");
                FillRequestByAuto(this);
                int deliverWay = 0x00;
                if (ChkPost.Checked)
                    deliverWay |= 0x80;
                if (ChkFax.Checked)
                    deliverWay |= 0x40;
                if (ChkFax.Checked)
                    deliverWay |= 0x20;
                request.SetRecordByName("TradePassword", frmPassword.Password);
                request.SetRecordByName("DeliverWay", Convert.ToString(deliverWay, 2).PadRight(8));

                //调用服务
                Log.LogInfo("开始调用服务");
                App.CallService("A101201", HandleResponse);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //释放请求包
                Log.LogDebug("释放请求包");
                request.Dispose();
            }
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            CloseFrame();
        }

        private void RadInvst_Click(object sender, EventArgs e)
        {

            if (sender == RadInvstIndi) //选择个人
            {
                SetCmbVal(CmbInvstType, "1");
                FillCmbByName(CmbCertType, "IndividualCertType");
            }
            else if (sender == RadInvstInst) //选择机构
            {
                SetCmbVal(CmbInvstType, "0");
                FillCmbByName(CmbCertType, "InstitutionCertType");
            }
        }

        private void FrmAcctTransOpen_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                FillCmbByAuto(this);
                FillCmbByName(CmbCertType, "IndividualCertType");
            }
        }

        private void TxtInvstName_TextChanged(object sender, EventArgs e)
        {
            TxtInvstAbbrName.Text = TxtInvstName.Text;
            TxtBankAcctName.Text = TxtInvstName.Text;
        }
    }
}
